package org.gdpu.action;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.sun.star.reflection.XPublished;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.gdpu.util.ChangeTime;
import org.gdpu.vo.FileVO;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 14-3-25.
 */
@Component("information")
@Scope("prototype")
public class InformationAction extends MyActionSupport implements ModelDriven<FileVO> {

    private final FileVO fileVO = new FileVO();
    private static String SAVE_PATH_DIR = "D:/Dormitory/_file/_information/";
    private final static Logger logger = LogManager.getLogger(InformationAction.class);

    public String delete(){
        for(String s : fileVO.getFileList()){
            File file = new File(SAVE_PATH_DIR + s);
            file.delete();
        }
        fileVO.setResultMessage("<script>alert('删除成功！');location.href='Information!fileList.action';</script>");
        return SUCCESS;
    }

    //TODO:在线查看文档，白云
    public String show(){
        System.out.println(SAVE_PATH_DIR+fileVO.getFileName());
        return SUCCESS;
    }

    public String fileList(){
        File file = new File(SAVE_PATH_DIR);
        String[] list = file.list();
        for(String s :list){
            List<String> sts = new ArrayList<String>();
            sts.add(s.substring(0,10));
            sts.add(s.substring(10));
            fileVO.getInformation().add(sts);
        }
        view = "/page/informationManagement/informationIndex.jsp";
        return VIEW;
    }

    public String upload(){
        // 得到保存上传文件的目录的真实路径
        File dir = new File(SAVE_PATH_DIR);
        // 如果该目录不存在，就创建
        if (!dir.exists()) {
            dir.mkdirs();
        }
        if (fileVO.getFileName().isEmpty()) {
            fileVO.setResultMessage("<script>alert('请选择文件！');location.href='/page/informationManagement/upload.jsp';</script>");
            return ERROR;
        }
        String[] temp = fileVO.getFileName().split("\\\\");
        String name = ChangeTime.getCurrentDate().substring(0,10) + temp[temp.length-1];
        try{
            FileInputStream is = new FileInputStream(new File(fileVO.getFileName()));
            FileOutputStream os = new FileOutputStream(new File(dir, name));
            byte[] buf = new byte[1024];
            int len = -1;
            while ((len = is.read(buf)) != -1) {
                os.write(buf, 0, len);
            }
            is.close();
            os.close();
        }catch(IOException e){
            logger.error(e);
            fileVO.setResultMessage("数据出错！");
            return ERROR;
        }
        fileVO.setResultMessage("主页成功生成通知链接，请查看！");
        return SUCCESS;
    }

    @Override
    public FileVO getModel() {
        return fileVO;
    }
}
